package utils 
{
	import flash.geom.Point;
	/**
	 * ...
	 * @author sailor
	 */
	public class Direction 
	{
		public static const North:int = 0;
		public static const NorthEast:int = 1;
		public static const East:int = 2;
		public static const SouthEast:int = 3;
		public static const South:int = 4;
		public static const SouthWest:int = 5;
		public static const West:int = 6;
		public static const NorthWest:int = 7;
		public static const NONE:int = -1;
	
		private static var increments:Vector.<Point> = Vector.<Point>([new Point(0, -1), new Point(1, -1), new Point(1, 0), new Point(1, 1),
		                                                               new Point(0,  1), new Point( -1, 1), new Point( -1, 0), new Point( -1, -1)]);
		public function Direction() 
		{
			
		}
		
		/**
		 * 根据指定的两个点获取方向
		 * @param	pos1
		 * @param	pos2
		 * @return
		 */
		public static function getDiction(pos1:Point, pos2:Point):int 
		{
			if (pos1.equals(pos2))
				return NONE;
			if (pos2.x > pos1.x)
			{
				if (pos2.y > pos1.y)
					return SouthEast;
				else if (pos2.y < pos1.y)
					return NorthEast;
				else 
					return East;
			}
			else if (pos2.x < pos1.x)
			{
				if (pos2.y > pos1.y)
					return SouthWest;
				else if (pos2.y < pos1.y)
					return NorthWest;
				else 
					return West;
			}
			else 
			{
				if (pos2.y < pos1.y)
					return North;
				else 
					return South;
			}
		}
		
		/**
		 * 根据方向获取运动增量
		 * @param	dir
		 * @return
		 */
		public static function getIncrement(dir:int):Point
		{
			return increments[dir];
		}
		
		/**
		 * 从运动增量获取方向
		 * @param	movement
		 * @return
		 */
		public static function getDirByIncrement(movement:Point):int
		{
			if (movement.x > 0)
			{
				if (movement.y < 0)
					return NorthEast;
				else if (movement.y > 0)
					return SouthEast;
				else 
					return East;
			}
			else if (movement.x < 0)
			{
				if (movement.y < 0)
					return NorthWest;
				else if (movement.y > 0)
					return SouthWest;
				else 
					return West;
			}
			else 
			{
				if (movement.y > 0)
					return South;
				else 
					return North;
			}
		}
		
	}

}